<?php
/*
 * @开发工具: JetBrains PhpStorm.
 * @文件名：CooperateAgency.php
 * @类功能: 友情链接管理
 * @开发者: gys
 * @开发时间： 15-10-22
 * @版本：version 1.0
 */
namespace app\admin\controller;

use app\common\cache\common\IFriendlyLinkCache;
use app\common\controller\AdminBaseController;
use app\common\enum\IsDelete;
use app\common\ext\IDb;
use app\common\ext\IRequest;

class FriendlyLink extends AdminBaseController {
    /**
     * @功能：友情链接列表
     * @开发者：gys
     */
    public function friendly_link_list(){
        $website_name = IRequest::get("website_name");
        $website_url = IRequest::get("website_url");
        $remark = IRequest::get("remark");

        $friendly_link_where = [];
        if(!empty($website_name)) $friendly_link_where['website_name'] = ['like',"%{$website_name}%"];
        if(!empty($website_url)) $friendly_link_where['website_url'] = ['like',"%{$website_url}%"];
        if(!empty($remark)) $friendly_link_where['remark'] = ['like',"%{$remark}%"];

        //获取友情链接列表
        $friendly_link_list = IDb::getInstance("friendly_link")->setDbWhere($friendly_link_where)->setDbOrder('order_by desc,friendly_link_id desc')->sel();

        //模板渲染
        $this->assign('friendly_link_list',$friendly_link_list);
        $this->assign('website_name',$website_name);
        $this->assign('website_url',$website_url);
        $this->assign('remark',$remark);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：友情链接添加
     * @开发者：gys
     */
    public function friendly_link_add(){
        //渲染模板输出
        return view();
    }

    /**
     * @功能：友情链接编辑
     * @开发者：gys
     */
    public function friendly_link_edit(){
        //接收参数
        $friendly_link_id = IRequest::get("friendly_link_id",IRequest::NOT_EMPTY,"友情链接编号不能为空！");

        //实例化对象
        $friendly_link_where['friendly_link_id'] = $friendly_link_id;
        $friendly_link_info = IDb::getInstance("friendly_link")->setDbWhere($friendly_link_where)->row();

        //模板渲染
        $this->assign('friendly_link_id',$friendly_link_id);
        $this->assign('friendly_link_info',$friendly_link_info);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：友情链接编辑提交
     * @开发者：gys
     */
    public function friendly_link_submit(){
        //接收参数
        $friendly_link_id = IRequest::get("friendly_link_id");
        $website_name = IRequest::get("website_name",IRequest::NOT_EMPTY,"友情链接名称不能为空！");
        $website_url = IRequest::get("website_url");
        $order_by = IRequest::get("order_by");
        $remark = IRequest::get("remark");

        $friendly_link_data['website_name'] = $website_name;
        $friendly_link_data['website_url'] = $website_url;
        $friendly_link_data['order_by'] = $order_by;
        $friendly_link_data['remark'] = $remark;

        //判断是修改还是添加
        if(empty($friendly_link_id)) {
            //添加友情链接编号
            $friendly_link_data['create_admin'] = get_login_admin_id();
            $friendly_link_data['create_time'] = get_date_time();
            $friendly_link_add = IDb::getInstance("friendly_link")->setDbData($friendly_link_data)->add();
            if($friendly_link_add === false){
                error('添加失败');
            }
        }else{
            //修改友情链接
            $friendly_link_data['update_admin'] = get_login_admin_id();
            $friendly_link_data['update_time'] = get_date_time();
            $friendly_link_where['friendly_link_id'] = array("eq",$friendly_link_id);
            $friendly_link_upd = IDb::getInstance("friendly_link")->setDbWhere($friendly_link_where)->setDbData($friendly_link_data)->upd();
            if($friendly_link_upd === false){
                error('修改失败！');
            }
        }

        //清除缓存
        IFriendlyLinkCache::deleteList();

        //画面跳转
        $this->success('操作成功',Url('friendly_link_list'));
    }

    /**
     * @功能：删除友情链接
     * @开发者：gys
     */
    public function friendly_link_del(){
        //接收参数
        $friendly_link_id= IRequest::get("friendly_link_id",IRequest::NOT_EMPTY,"友情链接编号不能为空！");

        $friendly_link_where['friendly_link_id'] = array("eq",$friendly_link_id);
        $data["is_delete"] = IsDelete::Yes;
        //删除友情链接
        $agency_upd = IDb::getInstance("friendly_link")->setDbWhere($friendly_link_where)->setDbData($data)->upd();
        if ($agency_upd === false) {
            error("删除友情链接失败！");
        }

        //清除缓存
        IFriendlyLinkCache::deleteList();

        //画面跳转
        $this->success('操作成功',Url('friendly_link_list'));
    }

    /**
     * @功能：还原友情链接
     * @开发者：gys
     */
    public function friendly_link_reset(){
        //接收参数
        $friendly_link_id= IRequest::get("friendly_link_id",IRequest::NOT_EMPTY,"友情链接编号不能为空！");

        $friendly_link_where['friendly_link_id'] = array("eq",$friendly_link_id);
        $data["is_delete"] = IsDelete::No;
        //还原友情链接
        $agency_upd = IDb::getInstance("friendly_link")->setDbWhere($friendly_link_where)->setDbData($data)->upd();
        if ($agency_upd === false) {
            error("还原友情链接失败！");
        }

        //清除缓存
        IFriendlyLinkCache::deleteList();

        //画面跳转
        $this->success('操作成功',Url('friendly_link_list'));
    }
}
